package com.yznote.cloudesk.user.controller;

import com.yznote.cloudesk.cache.yCache.YCache;
import com.yznote.cloudesk.common.Anno.CollectLogs;
import com.yznote.cloudesk.common.constants.ModuleConstant;
import com.yznote.cloudesk.common.constants.YCacheConstant;
import com.yznote.cloudesk.common.constants.YConstant;
import com.yznote.cloudesk.common.pojo.Result;
import com.yznote.cloudesk.common.pojo.User;
import com.yznote.cloudesk.common.tokenShare.YTokenShare;
import com.yznote.cloudesk.common.utils.YDUtils;
import com.yznote.cloudesk.user.mapper.UserMapper;
import com.yznote.cloudesk.user.server.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户系统")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    YTokenShare yTokenShare;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    YCache yCache;

    @ApiOperation("退出登录")
    @PostMapping("/logOff")
    //@CollectLogs
    public Result logOff() {
        yCache.delete(YCacheConstant.DESK,yTokenShare.get().getUserId().toString());
        return Result.success(null);
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUserMsg")
    public Result getUserMsg() {
        User user = userMapper.getUserMsg(yTokenShare.get().getUserId());
        return Result.success(user);
    }

    @ApiOperation("模糊搜索用户")
    @GetMapping("/getUserByName")
    public Result getUserByName(@RequestParam("userName") String userName) {
        List<User> userList = userService.getUserByName(userName);
        return Result.success(userList);
    }

    @ApiOperation("获取所有用户信息")
    @GetMapping("/getAllUsers")
    public Result getAllUsers(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        // 添加用户是否在线字段
        List<User> usersList = userService.getAllUsers(pageNum, pageSize);
        return Result.success(usersList);
    }

    @ApiOperation("获取所有在线用户信息")
    @GetMapping("/getAllOnlineUsers")
    public Result getAllOnlineUsers(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        List<User> usersList = userService.getAllOnlineUsers(pageNum,pageSize);
        return Result.success(usersList);
    }

    @ApiOperation("获取所有在线用户人数")
    @GetMapping("/getAllOnlineUsersCount")
    public Result getAllOnlineUsersCount(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return Result.success(userService.getAllOnlineUsersCount());
    }


}
